/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package bmodeling;

import java.io.*;
import java.net.*;
import java.util.jar.*;

/**
 * Object holding a {@link JarFile} and it's URL as String.
 * 
 * @author M. Hautle
 * @since 14.09.08
 */
class Jar {
	/** The jar file */
	final JarFile jar;

	/** The url of {@link #jar} */
	final String url;

	/**
	 * Default constructor
	 * 
	 * @param file The file pointing to the jar
	 * @throws IOException
	 */
	Jar(File file) throws IOException {
		jar = new JarFile(file);
		url = file.toURI().toURL().toExternalForm();
	}

	/**
	 * Looks up the entry with the given name.
	 * 
	 * @param name A file name (with / as package seperator)
	 * @param entry An {@link Entry} object where to put the result
	 * @return The passed entry object containing the resolved entry or null if nothing was found
	 */
	Entry getEntry(String name, Entry entry) {
		final JarEntry e = jar.getJarEntry(name);
		if (e == null)
			return null;
		entry.entry = e;
		entry.parent = this;
		return entry;
	}

	/**
	 * Returns the complete URL for the given entry.
	 * 
	 * @param entry The entry name
	 * @return The URL for this entry
	 * @throws MalformedURLException
	 */
	URL getEntryUrl(String entry) throws MalformedURLException {
		return new URL("jar", "", url + "!/" + entry);
	}
}